package net.suplementos.negocio;

import java.util.Date;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import net.suplementos.exception.CampoObrigatorioException;
import net.suplementos.exception.ErroInternoException;
import net.suplementos.exception.RegistroInexistenteException;
import net.suplementos.exception.RegistroJaExistenteException;
import net.suplementos.pojo.Promocao;
import net.suplementos.servico.MarketingServico;

public class MarketingDelegate implements MarketingServico {

    private static MarketingServico delegate;
    private InitialContext context;
    
    private MarketingDelegate() {
        try {
            context = new InitialContext();
        } catch (NamingException e) {
            throw new ErroInternoException(e.getMessage(), e);
        }
    }
    
    public static MarketingServico getInstance() {
        if (delegate == null) {
            delegate = new MarketingDelegate();
        }
        
        return delegate;
    }
    
    private MarketingServico getRemote() {
        try {
            return (MarketingServico) context.lookup("/net/suplementos/facade/Marketing/remote");
        } catch (NamingException e) {
            throw new ErroInternoException(e.getMessage(), e);
        }
    }


	public List<Promocao> consultar(String titulo, Date dataInicio, Date dataFim) throws CampoObrigatorioException {
	    return getRemote().consultar(titulo, dataInicio, dataFim);
	}

	public Promocao getPromocao(Integer id) {
	    return getRemote().getPromocao(id);
	}

	public void alterar(Promocao promocao) throws CampoObrigatorioException, RegistroJaExistenteException, RegistroInexistenteException {
	    getRemote().alterar(promocao);
	}

	public void salvar(Promocao promocao) throws CampoObrigatorioException, RegistroJaExistenteException {
	    getRemote().salvar(promocao);
	}

	public void contatarCliente(Promocao promocao) throws CampoObrigatorioException {
	    getRemote().contatarCliente(promocao);
	}
}
